﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;

namespace MikroMagazaObject
{
    public partial class frmPromoTrn : XtraForm
    {
        public byte[] PromoData { get; set; }
        public byte[] NoPromoData { get; set; }
        public string KasaYol { get; set; }
        public string KasaSayisi { get; set; }

        public frmPromoTrn()
        {
            InitializeComponent();
        }

        private void frmPromoTrn_Load(object sender, EventArgs e)
        {

        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            if (radioGroup1.SelectedIndex == 0)
            {
                PromoGonder();
            }
            else
            {
                NoPromoGonder();
            }
        }

        private void PromoGonder()
        {
            progressBarControl1.Properties.Minimum = 0;
            progressBarControl1.Properties.Maximum = 100;
            bool mesajGonder = false;
            string posgenel = KasaYol + "\\POSGENEL\\";
            if (this.PromoData != null)
            {
                mesajGonder = true;
                if (Directory.Exists(posgenel))
                {
                    File.WriteAllBytes(posgenel + "\\PROMO.DAT", this.PromoData);
                }
            }
            else
            {
                DialogResult dlg = MessageBox.Show("Promosyon bulunamadı ! Boş Promosyon göndermek istermisiniz ?", "Uyarı", MessageBoxButtons.YesNo);
                if (dlg == DialogResult.Yes)
                {
                    mesajGonder = true;
                    File.WriteAllText(posgenel + "\\PROMO.DAT", "");
                }
            }

            if (mesajGonder)
            {
                progressBarControl1.Position = 50;
                foreach (string kasa in KasaSayisi.Split(','))
                {
                    Mesaj_Yaz(5, false, int.Parse(kasa));
                }
                progressBarControl1.Position = 100;
            }
            else
            {
                progressBarControl1.Position = 0;
            }
        }

        private void NoPromoGonder()
        {
            progressBarControl1.Properties.Minimum = 0;
            progressBarControl1.Properties.Maximum = 100;
            bool mesajGonder = false;
            string posgenel = KasaYol + "\\POSGENEL\\";
            if (this.NoPromoData != null)
            {
                mesajGonder = true;
                if (Directory.Exists(posgenel))
                {
                    File.WriteAllBytes(posgenel + "\\NOPROMO.DAT", this.NoPromoData);
                }
            }
            else
            {
                DialogResult dlg = MessageBox.Show("NO Promosyon bulunamadı ! Boş Promosyon göndermek istermisiniz ?", "Uyarı", MessageBoxButtons.YesNo);
                if (dlg == DialogResult.Yes)
                {
                    mesajGonder = true;
                    File.WriteAllText(posgenel + "\\NOPROMO.DAT", "");
                }
            }

            if (mesajGonder)
            {
                progressBarControl1.Position = 50;
                foreach (string kasa in KasaSayisi.Split(','))
                {
                    Mesaj_Yaz(6, false, int.Parse(kasa));
                }
                progressBarControl1.Position = 100;
            }
            else
            {
                progressBarControl1.Position = 0;
            }
        }

        private void Mesaj_Yaz(int islem, bool tum, int kasa)
        {
            try
            {
                if (Directory.Exists(KasaYol + "\\POSKON\\") == true)
                {
                    StreamWriter sr = new StreamWriter(KasaYol + "\\POSKON\\" + "\\MESAJ." + kasa.ToString("000"), false);
                    switch (islem)
                    {
                        case 0:
                            if (tum)
                            {
                                sr.WriteLine("107100000000000");
                            }
                            else
                            {
                                sr.WriteLine("1140000000000000001");
                            }
                            break;
                        case 1:
                            sr.WriteLine("108100000000000");
                            break;
                        case 2:
                            sr.WriteLine("11400000000100000000000000");
                            break;
                        case 3:
                            sr.WriteLine("11400000000000000000000010");
                            break;
                        case 4:
                            sr.WriteLine("119000000000000");
                            break;
                        case 5:
                            sr.WriteLine("111000000000000");
                            break;
                        case 6:
                            sr.WriteLine("1350000000000");
                            break;
                    }
                    sr.Close();
                }
                else
                {
                    MessageBox.Show(KasaYol + "\\POSKON\\" + " Geçerli bir Dosya yolu Değil");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

    }
}
